package org.come.Jpanel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.math.BigDecimal;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import org.come.bean.JadeorGoodstableBean;
import org.come.mouslisten.IncludedPartsMpuslisten;
import org.come.mouslisten.IncludedSuitMouslisten;
import org.come.until.*;

import com.tool.btn.FormsOnOffBtn;
import com.tool.btn.SwitchPageBtn;
import com.tool.btn.WorkshopBtn;
import com.tool.role.RoleData;
import com.tool.tcpimg.UIUtils;
import com.updateNew.MyIsif;



public class AlreadyRecordedJpanel extends JPanel {

    public static int suitNum;// 已收录的套装数
    private SwitchPageBtn addNumBtn;// 加收录套装上限按钮
    private BigDecimal money,// 所需金钱
            sxlxz;// 所需灵修值
    private WorkshopBtn workshopBtn1,// 删除按钮
            workshopBtn2,// 收录按钮
            workshopBtn3,// 获得按钮
            workshopBtn4;// 生成玉符按钮

    private JTextField textField;// 生成个数输入框

    private JScrollPane jScrollPane, // 已收录的套装列表滚动条
            jScrollPane2; // 已收录的玉符数量滚动条

    private int suitid; // 存储选中的套装id
    private JLabel[] labSuitParts = new JLabel[9];
    private IncludedPartsMpuslisten[] mpuslisten = new IncludedPartsMpuslisten[9];
    private WorkshopBtn labAct;// 激活按钮

    private JList<String> listSuit; // 已收录的套装列表
    private DefaultListModel<String> listModel;// 放置套装的对象

    private JadeorGoodstableBean goodstableBean = new JadeorGoodstableBean(); // 存放要生成/激活的玉符

    public AlreadyRecordedJpanel() {

        this.setPreferredSize(new Dimension(552, 435));
        this.setLayout(null);
        this.setBackground(UIUtils.Color_BACK);

        // 关闭按钮事件
        FormsOnOffBtn offBtn = new FormsOnOffBtn("inkImg/old/2/1.png", 1, 65);
        offBtn.setBounds(552 - 37, 10, 25, 25);
        this.add(offBtn);

        listModel = new DefaultListModel<String>();
        // 已收录的套装列表
        listSuit = new JList<String>();
        listSuit.setOpaque(false);
        listSuit.setSelectionBackground(UIUtils.Color_BACK);
        listSuit.setSelectionForeground(Color.green);
        listSuit.setForeground(Color.white);
        listSuit.setFont(new Font("微软雅黑", 1, 14));
        listSuit.setBackground(UIUtils.Color_BACK); // 设置列表框为透明背景
        listSuit.setModel(listModel);
        listSuit.addMouseListener(new IncludedSuitMouslisten(this));

        // 已收录的套装列表滚动条
        jScrollPane = new JScrollPane(listSuit);
        jScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        jScrollPane.getVerticalScrollBar().setUI(new SrcollPanelUI());
        jScrollPane.getViewport().setOpaque(false);
        jScrollPane.setOpaque(false);
        jScrollPane.setBounds(47, 55, 166, 298);
        jScrollPane.setBorder(BorderFactory.createEmptyBorder());
        jScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        this.add(jScrollPane);

        // 已收录套装部件

        for (int i = 0, size = labSuitParts.length; i < size; i++) {
            labSuitParts[i] = new JLabel();
            if (i > 3) {
                labSuitParts[i].setBounds(245 + (i - 4) * 65, 136, 56, 55);
            } else {
                labSuitParts[i].setBounds(231 + i * 64, 68, 56, 55);
            }
            mpuslisten[i] = new IncludedPartsMpuslisten(i, this);
            labSuitParts[i].addMouseListener(mpuslisten[i]);
            this.add(labSuitParts[i]);
        }

        // 加收录套装上限按钮
        addNumBtn = new SwitchPageBtn("skin/8100226/button/16.png", 1, 11);
        addNumBtn.setBounds(194, 360, 18, 18);
        this.add(addNumBtn);

        // 生成个数输入框
        textField = new JTextField();
        textField.setBounds(328, 267, 141, 16);
        textField.setFont(UIUtils.TEXT_FONT1);
        textField.setOpaque(false);
        textField.setBorder(BorderFactory.createEmptyBorder());
        textField.setCaretColor(Color.white);
        textField.setForeground(Color.white);
        textField.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {
                // TODO Auto-generated method stub
                int charstr = e.getKeyChar();
                // control the value between 0 and 9
                if (charstr < KeyEvent.VK_0 || charstr > KeyEvent.VK_9) {
                    e.consume();
                    return;
                }
                String str = textField.getText();
                if (str.length() == 0) {
                    textField.setText("");
                }
                if (str.length() == 1) {
                    if (Long.parseLong(str) == 0) {
                        textField.setText("");
                    }
                }
                if (str.length() >= 3) {
                    e.consume();
                    return;
                } // 不超过100个生成数量
            }

            @Override
            public void keyPressed(KeyEvent e) {
            }

            @Override
            public void keyReleased(KeyEvent e) {

            }
        });
        textField.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void removeUpdate(DocumentEvent e) {
                String str = textField.getText();
                if (str != null && !"".equals(str)) {
                    if (Long.parseLong(str) > 0) {
                        money = new BigDecimal(Long.parseLong(str) * 10000000);
                        sxlxz = new BigDecimal(Long.parseLong(str) * 10);
                    }
                } else {
                    money = null;
                    sxlxz = null;
                }
            }

            @Override
            public void insertUpdate(DocumentEvent e) {
                String str = textField.getText();
                if (str != null && !"".equals(str)) {
                    if (Long.parseLong(str) > 0) {
                        money = new BigDecimal(Long.parseLong(str) * 10000000);
                        sxlxz = new BigDecimal(Long.parseLong(str) * 10);
                    }
                } else {
                    money = null;
                    sxlxz = null;
                }
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                String str = textField.getText();
                if (str != null && !"".equals(str)) {
                    if (Long.parseLong(str) > 0) {
                        money = new BigDecimal(Long.parseLong(str) * 10000000);
                        sxlxz = new BigDecimal(Long.parseLong(str) * 10);
                    }
                } else {
                    money = null;
                    sxlxz = null;
                }
            }
        });
        this.add(textField);

        // 删除按钮
        workshopBtn1 = new WorkshopBtn("skin/8100226/newbutton/B20.png", 1, UIUtils.COLOR_WHITE2, UIUtils.TEXT_HY16, "删除", 9,
                this);
        workshopBtn1.setBounds(53, 383, 59, 24);
        this.add(workshopBtn1);
        // 收录按钮
        workshopBtn2 = new WorkshopBtn("skin/8100226/newbutton/B20.png", 1, UIUtils.COLOR_WHITE2, UIUtils.TEXT_HY16, "收录",
                10, this);
        workshopBtn2.setBounds(129, 383, 59, 24);
        this.add(workshopBtn2);
        // 获得按钮
        workshopBtn3 = new WorkshopBtn("skin/8100226/button/2.png", 1, UIUtils.COLOR_BTNTEXT, UIUtils.TEXT_FONT, "获得", 11,
                this);
        workshopBtn3.setBounds(435, 357, 34, 17);
        this.add(workshopBtn3);
        // 生成玉符按钮
        workshopBtn4 = new WorkshopBtn("skin/8100226/newbutton/B22.png", 1, UIUtils.COLOR_WHITE2, UIUtils.TEXT_HY16, "生成玉符",
                13, this);
        workshopBtn4.setBounds(325, 381, 99, 24);
        this.add(workshopBtn4);

        // 激活按钮
        labAct = new WorkshopBtn("skin/8100226/button/2.png", 1, UIUtils.COLOR_BTNTEXT, UIUtils.TEXT_FONT, "激活", 15, this);
        labAct.setBounds(451, 207, 34, 17);
        labAct.setBtn(-1);
        labAct.setForeground(Color.GRAY);
        labAct.setIcon(CutButtonImage.getImage("skin/8100226/button/36.png", -1, -1));
        this.add(labAct);
    }

    private ImageIcon icon;

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (icon == null) {
            icon = new ImageIcon("inkImg/old/background/S99.png");
        }
        // 背景
        g.drawImage(icon.getImage(), 0, 0, 552, 435, this);

        // 画所需金钱
        if (money != null) {
            Util.drawPrice(g, money, 328, 297 + 13);
        }
        g.setFont(UIUtils.TEXT_FONT1);
        g.setColor(Color.white);
        // 画所需灵修值
        if (sxlxz != null) {
            g.drawString(sxlxz.toString(), 328, 327 + 13);
        }
        // 画拥有的灵修值
        if (RoleData.getRoleData().getLoginResult().getScoretype("灵修值") != null) {
            g.drawString(RoleData.getRoleData().getLoginResult().getScoretype("灵修值") + "", 328, 357 + 13);
        }
        // 画可收录套装数
        if (suitNum >= 0) {
            g.drawString(suitNum + "/" + RoleData.getRoleData().getPackRecord().getSuitNum(), 155, 361 + 13);
        }
        // 画已收录的套装部件
        GoodsListFromServerUntil.drawIncludedSuit(g, 232, 68, suitid);
    }

    /**
     * 清空界面
     */
    public void clearInterface() {
        textField.setText("");
        money = null;
        sxlxz = null;
        goodstableBean.setType(0);
        goodstableBean.setPartJade(null);
    }

    public JList<String> getListSuit() {
        return listSuit;
    }

    public void setListSuit(JList<String> listSuit) {
        this.listSuit = listSuit;
    }

    public DefaultListModel<String> getListModel() {
        return listModel;
    }

    public void setListModel(DefaultListModel<String> listModel) {
        this.listModel = listModel;
    }

    public int getSuitid() {
        return suitid;
    }

    public void setSuitid(int suitid) {
        this.suitid = suitid;
    }

    public BigDecimal getMoney() {
        return money;
    }

    public void setMoney(BigDecimal money) {
        this.money = money;
    }

    public BigDecimal getSxlxz() {
        return sxlxz;
    }

    public void setSxlxz(BigDecimal sxlxz) {
        this.sxlxz = sxlxz;
    }

    public JTextField getTextField() {
        return textField;
    }

    public void setTextField(JTextField textField) {
        this.textField = textField;
    }

    public JLabel[] getLabSuitParts() {
        return labSuitParts;
    }

    public void setLabSuitParts(JLabel[] labSuitParts) {
        this.labSuitParts = labSuitParts;
    }

    public IncludedPartsMpuslisten[] getMpuslisten() {
        return mpuslisten;
    }

    public void setMpuslisten(IncludedPartsMpuslisten[] mpuslisten) {
        this.mpuslisten = mpuslisten;
    }

    public WorkshopBtn getLabAct() {
        return labAct;
    }

    public void setLabAct(WorkshopBtn labAct) {
        this.labAct = labAct;
    }

    public JadeorGoodstableBean getGoodstableBean() {
        return goodstableBean;
    }

    public void setGoodstableBean(JadeorGoodstableBean goodstableBean) {
        this.goodstableBean = goodstableBean;
    }
}
